package com.puppet.controller.simple;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/consumer1")
@Slf4j
public class SimpleController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //手动消费
    @GetMapping("simpleQueue")
    public ResponseEntity<?> simpleQueue() {
        //接收消费者消费的信息
        String message = (String) rabbitTemplate.receiveAndConvert("simple.queue");
        //输出消息
        log.info("消费者接收到的消息：{}",message);
        return ResponseEntity.ok("消费者消费信息成功：" + message);
    }

    @GetMapping("simpleQueue1")
    @RabbitListener(queues = "simple.queue")
    public void simpleQueue1(String message) {
        //输出消息
        log.info("消费者接收到的消息：{}",message);
    }

    @GetMapping("simpleQueue2")
    @RabbitListener(queues = "simple.queue2")
    public void simpleQueue2(String message) {
        //输出消息
        log.info("消费者2接收到的消息：{}",message);
    }
}
